
using System;
using System.Threading;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using Reply.Seat.LeadCollector.BL;
using System.Text;
using Reply.Seat.LeadCollector.UI;

namespace Reply.Seat.LeadCollector.iPhone
{
	public partial class NuovoViewController : UIViewController
	{
		#region PRIVATE MEMBERS
		private Lead _currentLead;
		#endregion

		#region PUBLIC PROPERTIES
		public Lead CurrentLead {
			get {
				if (_currentLead == null)
					_currentLead = new Lead ();
				return _currentLead;
			}
		}

		#endregion

		#region Constructors

		// The IntPtr and initWithCoder constructors are required for items that need 
		// to be able to be created from a xib rather than from managed code

		public NuovoViewController (IntPtr handle) : base(handle)
		{
			Initialize ();
		}

		[Export("initWithCoder:")]
		public NuovoViewController (NSCoder coder) : base(coder)
		{
			Initialize ();
		}

		public NuovoViewController () : base("NuovoViewController", null)
		{
			Initialize ();
		}

		public NuovoViewController (Lead lead) : this()
		{
			this.nomeTxt.Text = lead.Nome;
			this.cognomeTxt.Text = lead.Cognome;
			this.emailTxt.Text = lead.IndirizzoEmail;
			this.numeroTelefonoTxt.Text = lead.NumeroTelefono;
			this.salvaBtn.Hidden = true;
			//this.aggiornaBtn.Hidden = false;
		}
		void Initialize ()
		{
		}

		#endregion

		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();
			this.salvaBtn.TouchDown += HandleSalvaBtnhandleTouchDown;
			this.nomeTxt.ShouldReturn = SharedCode.NuovoViewController.DoReturn;
			this.cognomeTxt.ShouldReturn = SharedCode.NuovoViewController.DoReturn;
			this.numeroTelefonoTxt.ShouldReturn = SharedCode.NuovoViewController.DoReturn;
			this.emailTxt.ShouldReturn = SharedCode.NuovoViewController.DoReturn;
			
			SharedCode.OnUpdateBadge +=  HandleSharedCodeOnUpdateBadge; 
		}

		void HandleSharedCodeOnUpdateBadge (object sender, EventArgs e)
		{
			((AppDelegate)UIApplication.SharedApplication.Delegate).UpdateNonSincronizzatiBadge ();
			
		}
		/// <summary>
		/// Metodo che permette di nascondere la tastiera al termine dall'editing del testo
		/// </summary>
		/// <param name="tf">
		/// A <see cref="UITextField"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.Boolean"/>
		/// </returns>
		

		void HandleSalvaBtnhandleTouchDown (object sender, EventArgs e)
		{
			SharedCode.NuovoViewController.HandleSalvaBtnhandleTouchDown(this,this.waitView,this.nomeTxt.Text,this.cognomeTxt.Text,this.numeroTelefonoTxt.Text,this.emailTxt.Text);
		}
	}
}


